package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

@Api(tags = "店铺相关接口")
@Slf4j
@RestController("adminShopController")
@RequestMapping("/admin/shop")
public class ShopController {
    @Autowired
    private RedisTemplate redisTemplate;

    private static final String KEY = "SHOP_STATUS";

    /**
     * 设置营业状态
     * @param status
     * @return
     */
    @ApiOperation("设置营业状态")
    @PutMapping("/{status}")
    public Result setStatus(@PathVariable Integer status){
        log.info("设置营业状态为: status={}", status);

        // 1.注入RedisTemplate对象,通过对象获取ValueOperations对象
        ValueOperations valueOperations = redisTemplate.opsForValue();
        // 2.通过ValueOperations对象操作String类型数据
        // 3.直接往redis中设置一个字符串类型,value就是状态值
        valueOperations.set(KEY, status);

        // 4.返回成功结果
        return Result.success();
    }

    /**
     * 查询店铺营业状态
     * @return
     */
    @ApiOperation("查询店铺营业状态")
    @GetMapping("/status")
    public Result<Integer> getStatus(){

        // 1.通过RedisTemplate对象获取ValueOperations对象
        ValueOperations valueOperations = redisTemplate.opsForValue();
        // 2.通过ValueOperations对象获取SHOP_STATUS
        Integer status = (Integer) valueOperations.get(KEY);
        log.info("获取营业状态:{}", status);

        // 3.将得到的value值响应给前端
        return Result.success(status);
    }
}
